/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sales.managedbean;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import sales.entity.LineItem;
import sales.entity.Product;
import sales.entity.SalesInquiry;
import sales.entity.SalesQuotation;
import sales.seesion.stateless.SalesInquirySessionBean;
import sales.seesion.stateless.SalesQuotationSessionBean;

/**
 *
 * @author frankxgz
 */
@ManagedBean
@ViewScoped
public class ToQuotationManagedBean implements Serializable {

    @EJB
    SalesInquirySessionBean inquirySessionBean;
    @EJB
    SalesQuotationSessionBean quotationSessionBean;
    private Long inquiryId;
    private SalesInquiry inquiry;
    private SalesQuotation quotation;
    private List<LineItem> lineItems = new ArrayList<LineItem>();
    private SalesQuotation selectedSalesQuotation;

    /** Creates a new instance of RaiseQuotationManagedBean */
    public ToQuotationManagedBean() {
    }

    public void addInquiryId(ActionEvent event) throws IOException {
        setInquiry(inquirySessionBean.getInquiryById(inquiryId));
        if (getInquiry() == null) {
            FacesContext.getCurrentInstance().getExternalContext().redirect("salesInquiryNotFound.xhtml");
        } else {
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("exInquiry", getInquiry());
            FacesContext.getCurrentInstance().getExternalContext().redirect("raiseSalesQuotation.xhtml");
        }
    }

    public void goBack() throws IOException {
        FacesContext.getCurrentInstance().getExternalContext().redirect("inquiryToQuotation.xhtml");
    }

    public void initView(PhaseEvent event) {

        if (quotation == null) {
            if (inquiry == null) {
                inquiry = (SalesInquiry) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("exInquiry");
            }
            quotation = new SalesQuotation();
            List<LineItem> lineItemList = inquiry.getInquiryLineItems();

            for (int i = 0; i < lineItemList.size(); i++) {
                getLineItems().add(lineItemList.get(i));
            }



            quotation.setCompany(inquiry.getCompany());

            List<Long> leftProducts = new ArrayList<Long>();
            if (!isIn(Long.valueOf(1L), lineItems)) {
                leftProducts.add(1L);
            }
            if (!isIn(Long.valueOf(2L), lineItems)) {
                leftProducts.add(2L);
            }
            if (!isIn(Long.valueOf(3L), lineItems)) {
                leftProducts.add(3L);
            }
            if (!isIn(Long.valueOf(4L), lineItems)) {
                leftProducts.add(4L);
            }

            for (Object o : leftProducts) {
                Long pId = (Long) o;
                Product pd = inquirySessionBean.getProduct(pId);
                LineItem li = new LineItem();
                li.setProduct(pd);
                li.setQuantity(0);
                lineItems.add(li);
            }
            quotation.setQuotationLineItems(lineItems);
        }
    }

    private boolean isIn(Long id, List<LineItem> itemList) {
        for (Object o : itemList) {
            LineItem li = (LineItem) o;
            if (id.equals(li.getProduct().getId())) {
                return true;
            }
        }
        return false;
    }

    public void proceed() throws IOException {
        for (int i = 0; i < quotation.getQuotationLineItems().size(); i++) {
            if (quotation.getQuotationLineItems().get(i).getQuantity() == 0) {
                quotation.getQuotationLineItems().remove(i);
                i = i - 1;
            }
        }
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("quotation", quotation);
        FacesContext.getCurrentInstance().getExternalContext().redirect("quotationForConfirm.xhtml");
    }

    public Long getInquiryId() {
        return inquiryId;
    }

    public void setInquiryId(Long inquiryId) {
        this.inquiryId = inquiryId;
    }

    public SalesInquiry getInquiry() {
        return inquiry;
    }

    public void setInquiry(SalesInquiry inquiry) {
        this.inquiry = inquiry;
    }

    public SalesQuotation getQuotation() {
        return quotation;
    }

    public void setQuotation(SalesQuotation quotation) {
        this.quotation = quotation;
    }

    public List<LineItem> getLineItems() {
        return lineItems;
    }

    public void setLineItems(List<LineItem> lineItems) {
        this.lineItems = lineItems;
    }

    public SalesQuotation getSelectedSalesQuotation() {
        return selectedSalesQuotation;
    }

    public void setSelectedSalesQuotation(SalesQuotation selectedSalesQuotation) {
        this.selectedSalesQuotation = selectedSalesQuotation;
    }
}